//
//  HWMineProtocol.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/28.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWMineProtocol: NSObject {
    
    var model:HWMineModel?
    
    var didClickItem:((_:HWMineDataModel)->())?
    var withdrawalButtonClick:((_:HWMineModel)->())?
    var rechargeButtonClick:((_:HWMineModel)->())?
    var logOut:(()->())?
    var didClickNotice:((_:HWMineNewsModel)->())?
}

extension HWMineProtocol:UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 || indexPath.section == 1 {

        }else if indexPath.section == (model?.nav.count ?? 0) + 2 {
            self.logOut?()
        }else {
            didClickItem?(model?.nav[indexPath.section - 2].data[indexPath.row] ?? HWMineDataModel())
        }
        
//        guard indexPath.section == 0 || indexPath.section == 1 || indexPath.section == (model?.nav.count ?? 0) + 2 else {
//            didClickItem?(model?.nav[indexPath.section - 2].data[indexPath.row] ?? HWMineDataModel())
//        }
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWMineHeaderView", for: indexPath) as! HWMineHeaderView
        if kind == UICollectionView.elementKindSectionHeader {
            header.model = model?.nav[indexPath.section - 2]
            return header;
        }else{
            return header;
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 || section == 1 || section == (model?.nav.count ?? 0) + 2 {
            return 1
        }
       
        return model?.nav[section-2].data.count ?? 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return (model?.nav.count ?? 0) + 3
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: screenWidth-40*widthScale, height: 233*widthScale)
        } else if indexPath.section == 1 {
            return CGSize(width: screenWidth-40*widthScale, height: 26*widthScale)
        }
        if indexPath.section == (model?.nav.count ?? 0) + 2 {
            return CGSize(width: screenWidth-40*widthScale, height: 44*widthScale)
        }
        return CGSize(width: (screenWidth-40*widthScale)/4, height: 50*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let topCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineTopCell", for: indexPath) as! HWMineTopCell
        let noticeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineNoticeCell", for: indexPath) as! HWMineNoticeCell
        let funcCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineFuncCell", for: indexPath) as! HWMineFuncCell
        let logOutCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineLogOutCell", for: indexPath) as! HWMineLogOutCell
        if indexPath.section == 0 {
            topCell.model = model
            topCell.withdrawalButtonClick = {
                self.withdrawalButtonClick?(self.model ?? HWMineModel())
            }
            topCell.rechargeButtonClick = {
                self.rechargeButtonClick?(self.model ?? HWMineModel())
            }
            return topCell
        }else if indexPath.section == 1 {
            noticeCell.models = model?.news
            noticeCell.didClickNotice = {
                mod in
                self.didClickNotice?(mod)
            }
            return noticeCell
        }
        if indexPath.section == (model?.nav.count ?? 0) + 2 {
            return logOutCell
        }
        funcCell.model = model?.nav[indexPath.section - 2].data[indexPath.row]
        return funcCell
        
    }
    
    
}

extension HWMineProtocol:JJCollectionViewDelegateRoundFlowLayout_Swift {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, configModelForSectionAtIndex section: Int) -> JJCollectionViewRoundConfigModel_Swift {
        
        let model = JJCollectionViewRoundConfigModel_Swift.init()
        model.backgroundColor = .white
        model.cornerRadius = 12*widthScale
        if section == 1 {
            model.backgroundColor = .colorWithHexString(hexString: "#DEE4FF")
        }
        
        return model
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, borderEdgeInsertsForSectionAtIndex section: Int) -> UIEdgeInsets {
        if section == (model?.nav.count ?? 0) + 2 {
            return UIEdgeInsets(top: 5*widthScale, left: 20*widthScale, bottom: 10*widthScale, right: 20*widthScale)
        }
        return UIEdgeInsets(top: 5*widthScale, left: 20*widthScale, bottom: 5*widthScale, right: 20*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 0{
            return UIEdgeInsets.zero
        } else if section == 1 {
            return UIEdgeInsets(top: 5*widthScale, left: 0, bottom: 5*widthScale, right: 0)
        }else if section == (model?.nav.count ?? 0) + 2 {
            return UIEdgeInsets(top: 5*widthScale, left: 0, bottom: 10*widthScale, right: 0)
        }
        return UIEdgeInsets(top: 20*widthScale, left: 20*widthScale, bottom: 20*widthScale, right: 20*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 || section == 1 || section == (model?.nav.count ?? 0) + 2{
            return CGSize.zero
        }
        return CGSize(width: screenWidth-40*widthScale, height: 40*widthScale)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        
        return CGSize.zero
    }
    
    
    
}
